package com.kaka.bus.engine.amount.creditamount;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kaka.bus.R;
import com.kaka.bus.adapter.ManageAmountAdapter;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.BpmList;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.LoanBean;
import com.kaka.bus.bean.ProductDetailBean;
import com.kaka.bus.bean.Status;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.amount.gongjijinamount.GongJiJinActivity;
import com.kaka.bus.engine.amount.loanproduct.LoanProductActivity;
import com.kaka.bus.engine.home.main.limit.LimitActivity;
import com.kaka.bus.engine.user.login.LoginActivity;
import com.kaka.bus.util.U;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ManageamountActivity extends BaseActivity<ManageamountPresenter> implements ManageamountContract.View {
    public static String LOANKEY = "Loankey";
    public static String MONEY = "moneymoney";
    public static int REQUESTCODE = 0;
    private float mMoney = 0;
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.tv_rmb)
    TextView tv_rmb;
    @BindView(R.id.tv_subtitle)
    TextView tv_subtitle;

    @BindView(R.id.tv_amount)
    TextView tv_amount;


    @BindView(R.id.main_recy)
    RecyclerView mMainRecy;
    @BindView(R.id.amount_btn)
    AppCompatButton amount_btn;

    @BindView(R.id.main_swipe)
    SwipeRefreshLayout mMainSwipe;

    private boolean nodeStatusError = false;
    LoanBean loanBean;
    private int type;
    ManageAmountAdapter manageAmountAdapter;
    List<ProductDetailBean> beanList;

    @Override
    protected void initView() {
        super.initView();
        mMainSwipe.setProgressViewEndTarget(false, 300);
        mMainSwipe.setColorSchemeColors(getResources().getColor(R.color.toolbar_color));

        mMoney = getIntent().getFloatExtra(MONEY, 0);
        if (mMoney > 0) {
            amount_btn.setText("管理额度");
        }
        mToolbarBack.setOnClickListener(v -> finish());
        amount_btn.setOnClickListener(view -> {
            Intent intent = new Intent(this, LimitActivity.class);
            intent.putExtra(LimitActivity.LIMIT, mMoney);
            startActivityForResult(intent, REQUESTCODE);
        });


    }

    @Override
    protected void initListener() {
        super.initListener();
        mMainSwipe.setOnRefreshListener(this::refreshUserStatus);
        manageAmountAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ProductDetailBean bean = beanList.get(position);
                if (TextUtils.isEmpty(getUserId())) {
                    startActivity(new Intent(ManageamountActivity.this, LoginActivity.class));
                } else {
                    if (loanBean.getProductType() == 3) {
                        startActivity(new Intent(ManageamountActivity.this, GongJiJinActivity.class));
                    } else {
                        Intent intent = new Intent(ManageamountActivity.this, LoanProductActivity.class);
                        Bundle bundle = new Bundle();
                        ProductDetailBean ProductDetailBean1 = (ProductDetailBean) adapter.getData().get(position);
//                        bundle.putParcelable(LoanProductActivity.PROKEY, ProductDetailBean1);
//                        intent.putExtras(bundle);
                        intent.putExtra(LoanProductActivity.TYPE, type);
                        intent.putExtra(LoanProductActivity.PROID, ProductDetailBean1.getProductId());
                        intent.putExtra(LoanProductActivity.MONEY, mMoney);
                        startActivity(intent);
                    }

                }


            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUESTCODE && requestCode == LimitActivity.REQUESTCODE) {
            float v = data.getFloatExtra("money", 0);
            if (v == 0) {
                tv_amount.setText("暂无额度");
                tv_rmb.setVisibility(View.GONE);
            } else {
                tv_amount.setText("￥" + (int) mMoney);
                tv_rmb.setVisibility(View.VISIBLE);
            }
        }
    }

    public void refreshUserStatus() {
        if (getUserId() != null) {
            mPresenter.loadProduct(getUserId(), getToken(), Constant.PLATFORM, loanBean.getId(), U.getAppInfo(ManageamountActivity.this).getVersionName());

        } else {
            closeRefresh();
        }
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.checkBpm(getUserId(), getToken());
        if (mMoney == 0) {
            tv_amount.setText("暂无额度");
            tv_rmb.setVisibility(View.GONE);
        } else {
            tv_amount.setText("￥" + (int) mMoney);
            tv_rmb.setVisibility(View.VISIBLE);
        }
        beanList = new ArrayList<>();
        loanBean = getIntent().getParcelableExtra(LOANKEY);
        mToolbarName.setText(loanBean.getProductName());
        tv_subtitle.setText(loanBean.getProductName() + "预估额度(元)");
        type = loanBean.getProductType();
        manageAmountAdapter = new ManageAmountAdapter(this, R.layout.item_manageamount, beanList);
        mMainRecy.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mMainRecy.setAdapter(manageAmountAdapter);
        mPresenter.loadProduct(getUserId(), getToken(), Constant.PLATFORM, loanBean.getId(), U.getAppInfo(this).getVersionName());

    }


    @Override
    protected boolean needFullScreen() {
        return true;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }


    @Override
    public void getLoadProduct(HttpWrapper<List<ProductDetailBean>> listHttpWrapper) {
        closeRefresh();
        if (Constant.TOKEN_TIMEOUT.equals(listHttpWrapper.code)) {
            U.tokenOut(this);
//        mMainProAdapter.bindToRecyclerView(mMainRecy);
        } else if (Constant.SUCCESS_CODE.equals(listHttpWrapper.code)) {
            beanList.clear();
            beanList.addAll(listHttpWrapper.getData());
            manageAmountAdapter.notifyDataSetChanged();
        } else {
            showMessage(listHttpWrapper.info);
        }

    }

    @Override
    public void getLimitSuccess(HttpWrapper<List<BpmList>> listHttpWrapper) {
//        if (Constant.SUCCESS_CODE.equals(listHttpWrapper.code)) {
//            for (int i = 0; i < listHttpWrapper.getData().size(); i++) {
//                if (listHttpWrapper.getData().get(i).getNodeStatus().equals("NS001")) {
//                    nodeStatusError = true;
//                }
//            }
//            if (nodeStatusError) {
//                tv_amount.setText("暂无额度");
//            } else {
//                tv_amount.setText(mMoney + "");
//            }
//        } else if (Constant.TOKEN_TIMEOUT.equals(listHttpWrapper.code)) {
//            U.tokenOut(this);
//        } else {
//            showMessage(listHttpWrapper.info);
//        }
    }

    @Override
    public void checkBpmSuccess(HttpWrapper<Status> s) {
        if (Constant.TOKEN_TIMEOUT.equals(s.code)) {
            U.tokenOut(this);
        } else if (Constant.NODE_OK.equals(s.code) || Constant.SUCCESS_CODE.equals(s.code)) {
            mMoney = Float.parseFloat(s.getData().getAmount());
            tv_amount.setText("￥" + (int) mMoney);
            tv_rmb.setVisibility(View.VISIBLE);
        } else {
            tv_amount.setText("暂无额度");
            tv_rmb.setVisibility(View.GONE);
        }
    }


    public void closeRefresh() {
        if (mMainSwipe == null) {
            return;
        }

        if (mMainSwipe.isRefreshing()) {
            mMainSwipe.setRefreshing(false);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_manage_amount_main2;
    }
}
